﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;
using System.Collections.Generic;
using Mani.Common;

public partial class SalesPersons : BasePage {

    #region"Private Property"
    private ISalesPersonService salesPersonService;
    static int numberDiv = 0;
    #endregion
    #region"Public Method"
    public ISalesPersonService SalesPersonService
    {
        get { return salesPersonService; }
        set { salesPersonService = value; }
    }
    public override string ActionName
    {
        get
        {
            return "vw_salespersons";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        bindGrid();
    }
    #endregion

    #region"Private Method"
    private void bindGrid()
    {
        IList<SalesPerson> li = SalesPersonService.FindAll();
        
        gvSalesPersons.DataSource = li;
        gvSalesPersons.DataBind();
    }
    #endregion

    protected void gvSalesPersons_RowDeleting(object sender, GridViewDeleteEventArgs e) {
        int Id = Convert.ToInt32(gvSalesPersons.DataKeys[e.RowIndex].Value);

        try {
            salesPersonService.Delete(Id);
            bindGrid();
        }
        catch {
            UIHelper.ClientAlert(this.Page, "امکان حذف فروشنده وجود ندارد!");
        }

        lblMessage.Text = "";
    }

    protected void gvSalesPersons_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
        int Id = Convert.ToInt32(gvSalesPersons.DataKeys[e.NewSelectedIndex].Value);

        SalesPerson s = salesPersonService.FindById(Id);
        txtSalesPersonAddress.Text = s.Address;
        txtSalesPersonPhoneNumber.Text = s.Phone;
        txtSalesPersonTitle.Text = s.Title;
        txtSalesPersonId.Text = s.Id.ToString();
        lblMessage.Text = "";
    }

    protected void btnSaveSalesPerson_Click(object sender, EventArgs e) {
        SalesPerson s = salesPersonService.FindById(Convert.ToInt32(txtSalesPersonId.Text));

        if (s == null)
            return;

        s.Address = txtSalesPersonAddress.Text;
        s.Phone = txtSalesPersonPhoneNumber.Text;
        s.Title = txtSalesPersonTitle.Text;
        s.ModifyStamp = DateTime.Now;

        salesPersonService.SaveOrUpadate(s);
        lblMessage.Text = "تغییرات با موفقیت ذخیره شد";
        ResetForm();
        bindGrid();
    }

    private void ResetForm() {
        txtSalesPersonAddress.Text = "";
        txtSalesPersonPhoneNumber.Text = "";
        txtSalesPersonTitle.Text = "";
        txtSalesPersonId.Text = "";
    }

    protected void gvSalesPersons_Sorting(object sender, GridViewSortEventArgs e)
    {
        bool dir;
        if ((numberDiv % 2) == 0)
            dir = true;
        else
            dir = false;
        numberDiv++;
        gvSalesPersons.DataSource = SalesPersonService.SortSalesPerson(e.SortExpression, dir);
        gvSalesPersons.DataBind();
    }
}
